home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / dcopy332.zip / DCDEST.C < prev    next >
C/C++ Source or Header  |  1990-04-14  |  4KB  |  114 lines

  1. /*************************************************************
  2.  *                                                           *
  3.  * Program DCOPY - physical sector to sector copy              *
  4.  * Copyright (c) 1986 Joerg Genius, Munich, West-Germany     *
  5.  *                                                           *
  6.  *************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <alloc.h>
  10. #include <sys\types.h>
  11. #include <sys\stat.h>
  12. #include "dcdefs.h"
  13.  
  14.  
  15. dfdestination (f_name,von,nach,nocompress)
  16.  
  17. int von,nocompress;
  18. FILE *nach;
  19. char *f_name;
  20.  
  21. {
  22.    struct drive_data source;
  23.    unsigned int blcnt,seccnt;
  24.    register unsigned int ccnt,current;
  25.    int error;
  26.    unsigned char *freesec; /* SECTOR BITMAP */
  27.    unsigned char *freewhat;
  28.    unsigned char *dsk_buf;
  29.    unsigned int sec_maps;
  30.    unsigned int w_count=0;
  31.    unsigned int max_blocks;
  32.  
  33.  
  34.    if (get_drive_data(von,&source)!=0)
  35.       return 1;
  36.    if (nocompress==0 && blk_p_buffer>64)
  37.       blk_p_buffer=64;
  38.    max_blocks=get_max_blocks(source.sec_p_drive);
  39.    printf (text[8],d_types[disk_type]);
  40.    printf (text[9],source.sec_p_drive,von+'A',f_name);
  41.    if (nocompress) {
  42.       for (blcnt=0;blcnt<source.sec_p_drive;blcnt+=max_blocks) {
  43.          max_blocks=(blcnt+max_blocks>=source.sec_p_drive ? source.sec_p_drive-blcnt : max_blocks);
  44.          if ((error=read_block(von,blcnt,max_blocks,disk_buffer))!=0) {
  45.             fprintf (stderr,text[10],error,err_text[error]);
  46.             return 1;
  47.          }
  48.          if (fwrite(disk_buffer,512,max_blocks,nach)!=max_blocks) {
  49.             fprintf (stderr,text[11]);
  50.             return 1;
  51.          }
  52.       }
  53.       printf (text[12],source.sec_p_drive,von+'A',f_name);
  54.       return 0;
  55.    }
  56.    if ((freesec=malloc((source.sec_p_drive+7)/8))==NULL ||
  57.        (freewhat=malloc(source.sec_p_drive))==NULL) {
  58.       fprintf (stderr,text[2]);
  59.       return 1;
  60.    }
  61.    sec_maps=(source.sec_p_drive+7)/8;
  62.    for (blcnt=0;blcnt<sec_maps;freesec[blcnt++]=0);
  63.    if (fwrite(&source.sec_p_drive,2,1,nach)!=1) {
  64.       fprintf (stderr,text[11]);
  65.       return 1;
  66.    }
  67.    if (fwrite(freesec,1,sec_maps,nach)!=sec_maps) {
  68.       fprintf (stderr,text[11]);
  69.       return 1;
  70.    }
  71.    if (fwrite(freewhat,1,source.sec_p_drive,nach)!=source.sec_p_drive) {
  72.       fprintf (stderr,text[11]);
  73.       return 1;
  74.    }
  75.    for (blcnt=0;blcnt<source.sec_p_drive;blcnt+=max_blocks) {
  76.       max_blocks=(blcnt+max_blocks>=source.sec_p_drive ? source.sec_p_drive-blcnt : max_blocks);
  77.       if ((error=read_block(von,blcnt,max_blocks,disk_buffer))!=0) {
  78.          fprintf (stderr,text[10],error,err_text[error]);
  79.          return 1;
  80.       }
  81.       dsk_buf=disk_buffer;
  82.       for (seccnt=blcnt;seccnt<blcnt+max_blocks;seccnt++) {
  83.      current=dsk_buf[0];
  84.      ccnt=0;
  85.      while (ccnt<512 && dsk_buf[ccnt++]==current);
  86.      if (ccnt<512) {
  87.             w_count++;
  88.             if (fwrite(dsk_buf,1,512,nach)!=512) {
  89.                fprintf (stderr,text[11]);
  90.                return 1;
  91.             }
  92.      }
  93.      else {
  94.             freesec[seccnt/8]|=1<<((seccnt)%8);
  95.         freewhat[seccnt]=current;
  96.      }
  97.          dsk_buf+=512;
  98.       }
  99.    }
  100.    fseek(nach,(long)2,0);
  101.    if (fwrite(freesec,1,sec_maps,nach)!=sec_maps) {
  102.       fprintf (stderr,text[11]);
  103.       return 1;
  104.    }
  105.    if (fwrite(freewhat,1,source.sec_p_drive,nach)!=source.sec_p_drive) {
  106.       fprintf (stderr,text[11]);
  107.       return 1;
  108.    }
  109.    printf (text[12],source.sec_p_drive,von+'A',f_name);
  110.    printf (text[13],(long)(source.sec_p_drive-w_count)*(long)512-(long)(sec_maps+source.sec_p_drive+2));
  111.    return 0;
  112. }
  113.  
  114.